using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using dbvc.context.Security;
using dbvc.data;
using dbvc.Queries;
using WGT.Extensions;

namespace App_Code.Context
{
    public class dbvc_model
    {
        public Security Security = new Security();

        private int project_;
        private int version_;

        private DAA db;

        public Query<db_script> qScript;
        public Query<db_user> qUser;

        public dbvc_model()
        {
            db = new DAA();
            qScript = new Query<db_script>();

            qScript.PageSize = 20;
            qScript.PageIndex = 1;

            qScript.Sort = "name";
            qScript.IsAsc = true;

            qScript.Source =
                db.GetTable<db_version_script>()
                    .Where(vs => version_ == vs.id_version)
                    .Join(db.GetTable<db_script>(), x => x.id_script, x => x.id, (vs, s) => s);

            qUser = new Query<db_user>();
            qUser.Sort = "name";
            qUser.PageSize = 101;
            qUser.PageIndex = 1;
            qUser.IsAsc = true;
            qUser.Source = db.GetTable<db_user>();
        }

        public int Project
        {
            get { return project_; }
            set
            {
                project_ = value;
                Version = 0;
            }
        }

        public int Version
        {
            get { return version_; }
            set
            {
                var old = version_;
                version_ = value;
                if (old != version_) qScript.Execute();
            }
        }

        public int Script { get; set; }

        //public List<db_script> Scripts { get; set; }

        //public void Reload_Scripts()
        //{
        //    Scripts = qScripts.Execute().ToList();
        //    Script = 0;
        //}
    }
}